package com.wyth.lottery.utils;

import com.wyth.lottery.base.*;
import com.wyth.lottery.bean.SSQDataBean;
import com.wyth.lottery.bean.SSQSelectBean;
import com.wyth.lottery.service.Entity.SSQBZData;
import com.wyth.lottery.service.Entity.SSQDTData;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by phh on 2017/10/27.
 * 各种工具类
 */

public class Number {
    /**
     * 需要在小于10的个数前面加上0的需要调用
     *
     * @param number 总共的个数
     * @param mlist
     * @return
     */
    public static void getList(int number, ArrayList<SSQSelectBean> mlist) {
        for (int i = 0; i < number; i++) {
            SSQSelectBean redbean = new SSQSelectBean();
            String str = 0 + "";
            if (i < 9) {
                redbean.text = str + (i + 1);
            } else {
                redbean.text = i + 1 + "";
            }

//            redbean.selectBoolean = true;
            redbean.selectBoolean = false;
            redbean.isBoolean = true;
            redbean.setClick(true);
            mlist.add(redbean);
        }
    }

    public static String getSSQData() {
        List<SSQDataBean> mlist = SaveUtils.getGoodsInfos().mlist;
        List<SSQBZData> mBZ = new ArrayList<>();
        List<SSQDTData> mDT = new ArrayList<>();
        getType(mlist, mBZ, mDT);
        //开始拼接
        StringBuffer SB = new StringBuffer();
        for (int i = 0; i < mBZ.size(); i++) {

            SSQBZData ssqbzData = mBZ.get(i);
            List<SSQSelectBean> red = ssqbzData.getRed();
            List<SSQSelectBean> blue = ssqbzData.getBlue();
            int number = ssqbzData.getNumber();
            setBZ(mBZ, SB, i, red, blue, number);
        }
        if (mDT.size() != 0 && mBZ.size() != 0) {
            SB.append(",");
        } else {

        }
        if (mDT != null) {
            for (int i = 0; i < mDT.size(); i++) {
                SSQDTData ssqdtData = mDT.get(i);
                if (i == mDT.size() - 1) {
                    OnesetDT(SB, ssqdtData);
                } else {
                    NoOnesetDT(SB, ssqdtData);
                }
            }

        }
        String s = SB.toString();


        return s;
    }

    private static void OnesetDT(StringBuffer SB, SSQDTData ssqdtData) {
        SB.append("{");
        SB.append("\"");
        SB.append("buyNumber");
        SB.append("\"");
        SB.append(":");
        SB.append("\"");
        List<SSQSelectBean> redTM1 = ssqdtData.getRedTM();
        List<SSQSelectBean> redDM = ssqdtData.getRedDM();

        List<SSQSelectBean> blue = ssqdtData.getBlue();
        int number = ssqdtData.getNumber();

        for (int i = 0; i < redTM1.size(); i++) {
            SSQSelectBean ssqSelectBean = redTM1.get(i);
            if (i == redTM1.size() - 1) {
                SB.append(ssqSelectBean.getText());
            } else {
                SB.append(ssqSelectBean.getText() + ",");
            }
        }

        SB.append("@");
        for (int i = 0; i < redDM.size(); i++) {
            SSQSelectBean ssqSelectBean = redDM.get(i);
            if (i == redDM.size() - 1) {
                SB.append(ssqSelectBean.getText());
            } else {
                SB.append(ssqSelectBean.getText() + ",");
            }
        }
        SB.append("#");
        for (int i = 0; i < blue.size(); i++) {
            SSQSelectBean ssqSelectBean = blue.get(i);
            if (i == blue.size() - 1) {
                SB.append(ssqSelectBean.getText());
            } else {
                SB.append(ssqSelectBean.getText() + ",");
            }

        }

        SB.append("\",\"");
        SB.append("playId\":\"01\",\"pollId\":\"03\",\"item\":\"" + number / 2 + "\",\"amount\":");
        SB.append(number + "}");
    }

    private static void NoOnesetDT(StringBuffer SB, SSQDTData ssqdtData) {
        SB.append("{");
        SB.append("\"");
        SB.append("buyNumber");
        SB.append("\"");
        SB.append(":");
        SB.append("\"");
        List<SSQSelectBean> redTM1 = ssqdtData.getRedTM();
        List<SSQSelectBean> redDM = ssqdtData.getRedDM();

        List<SSQSelectBean> blue = ssqdtData.getBlue();
        int number = ssqdtData.getNumber();

        for (int i = 0; i < redTM1.size(); i++) {
            SSQSelectBean ssqSelectBean = redTM1.get(i);
            if (i == redTM1.size() - 1) {
                SB.append(ssqSelectBean.getText());
            } else {
                SB.append(ssqSelectBean.getText() + ",");
            }
        }

        SB.append("@");
        for (int i = 0; i < redDM.size(); i++) {
            SSQSelectBean ssqSelectBean = redDM.get(i);
            if (i == redDM.size() - 1) {
                SB.append(ssqSelectBean.getText());
            } else {
                SB.append(ssqSelectBean.getText() + ",");
            }
        }
        SB.append("#");
        for (int i = 0; i < blue.size(); i++) {
            SSQSelectBean ssqSelectBean = blue.get(i);
            if (i == blue.size() - 1) {
                SB.append(ssqSelectBean.getText());
            } else {
                SB.append(ssqSelectBean.getText() + ",");
            }

        }

        SB.append("\",\"");
        SB.append("playId\":\"01\",\"pollId\":\"03\",\"item\":\"" + number / 2 + "\",\"amount\":");
        SB.append(number + "},");
    }

    private static void setBZ(List<SSQBZData> mBZ, StringBuffer SB, int i, List<SSQSelectBean> red, List<SSQSelectBean> blue, int number) {
        if (red.size() > 6 || blue.size() > 1) {
            getSSQText02(mBZ, SB, i, red, blue, number);
        } else {
            getSSQText(mBZ, SB, i, red, blue, number);
        }
    }

    private static void getSSQText02(List<SSQBZData> mBZ, StringBuffer SB, int i, List<SSQSelectBean> red, List<SSQSelectBean> blue, int number) {
        if (i == mBZ.size() - 1) {
            SB.append("{");
            SB.append("\"");
            SB.append("buyNumber");
            SB.append("\"");
            SB.append(":");
            SB.append("\"");
            for (int i1 = 0; i1 < red.size(); i1++) {
                if (i1 == red.size() - 1) {
                    SB.append(red.get(i1).getText() + "#");
                } else {
                    SB.append(red.get(i1).getText() + ",");
                }
            }
            for (int i2 = 0; i2 < blue.size(); i2++) {
                if (i2 == blue.size() - 1) {
                    SB.append(blue.get(i2).getText());
                } else {
                    SB.append(blue.get(i2).getText() + ",");
                }
            }
            SB.append("\",\"");
            SB.append("playId\":\"01\",\"pollId\":\"02\",\"item\":\"" + number / 2 + "\",\"amount\":");
            SB.append(number + "}");

        } else {
            SB.append("{");
            SB.append("\"");
            SB.append("buyNumber");
            SB.append("\"");
            SB.append(":");
            SB.append("\"");
            for (int i1 = 0; i1 < red.size(); i1++) {
                if (i1 == red.size() - 1) {
                    SB.append(red.get(i1).getText() + "#");
                } else {
                    SB.append(red.get(i1).getText() + ",");
                }
            }
            for (int i2 = 0; i2 < blue.size(); i2++) {
                if (i2 == blue.size() - 1) {
                    SB.append(blue.get(i2).getText());
                } else {
                    SB.append(blue.get(i2).getText() + ",");
                }

            }
            SB.append("\",\"");
            SB.append("playId\":\"01\",\"pollId\":\"02\",\"item\":\"1\",\"amount\":");
            SB.append(number + "},");
        }
    }

    private static void getSSQText(List<SSQBZData> mBZ, StringBuffer SB, int i, List<SSQSelectBean> red, List<SSQSelectBean> blue, int number) {
        if (i == mBZ.size() - 1) {
            SB.append("{");
            SB.append("\"");
            SB.append("buyNumber");
            SB.append("\"");
            SB.append(":");
            SB.append("\"");
            for (int i1 = 0; i1 < red.size(); i1++) {
                if (i1 == red.size() - 1) {
                    SB.append(red.get(i1).getText() + "#");
                } else {
                    SB.append(red.get(i1).getText() + ",");
                }
            }
            for (int i2 = 0; i2 < blue.size(); i2++) {
                SB.append(blue.get(i2).getText());
            }
            SB.append("\",\"");
            SB.append("playId\":\"01\",\"pollId\":\"01\",\"item\":\"1\",\"amount\":");
            SB.append(number + "}");

        } else {
            SB.append("{");
            SB.append("\"");
            SB.append("buyNumber");
            SB.append("\"");
            SB.append(":");
            SB.append("\"");
            for (int i1 = 0; i1 < red.size(); i1++) {
                if (i1 == red.size() - 1) {
                    SB.append(red.get(i1).getText() + "#");
                } else {
                    SB.append(red.get(i1).getText() + ",");
                }
            }
            for (int i2 = 0; i2 < blue.size(); i2++) {
                SB.append(blue.get(i2).getText());
            }
            SB.append("\",\"");
            SB.append("playId\":\"01\",\"pollId\":\"01\",\"item\":\"1\",\"amount\":");
            SB.append(number + "},");
        }
    }

    private static void getType(List<SSQDataBean> mlist, List<SSQBZData> mBZ, List<SSQDTData> mDT) {
        for (SSQDataBean ssqDataBean : mlist) {
            String type = ssqDataBean.getType();

            if (type.equals("标准投注")) {
                mBZ.add(new SSQBZData(ssqDataBean.getRed(), ssqDataBean.getMblue(), ssqDataBean.getMoney()));

            } else if (type.equals("胆拖投注")) {
                List<SSQSelectBean> mRedDM = new ArrayList<>();
                List<SSQSelectBean> mRedTM = new ArrayList<>();
                List<SSQSelectBean> mBlue = new ArrayList<>();

                List<SSQSelectBean> red = ssqDataBean.getRed();
                for (SSQSelectBean ssqSelectBean : red) {
                    boolean selectBoolean = ssqSelectBean.selectBoolean;
                    boolean isBoolean = ssqSelectBean.isBoolean;
                    if (selectBoolean == true && isBoolean == true) {
                        mRedDM.add(new SSQSelectBean(ssqSelectBean.getText(), true));
                    } else if (selectBoolean == true && isBoolean == false) {
                        mRedTM.add(new SSQSelectBean(ssqSelectBean.getText(), false));
                    }
                }
                mBlue.addAll(ssqDataBean.getMblue());

                mDT.add(new SSQDTData(mRedDM, mRedTM, mBlue, ssqDataBean.getMoney()));
            }
        }
    }


}
